﻿using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 干涉检查Demo;

namespace  SqlData
{
    [Persistent(nameof(SingleNotCalculated))]
    [OptimisticLocking(true), Browsable(true)]
    public class SingleNotCalculated : XPBaseObject
    {
        public SingleNotCalculated(Session session) : base(session) { }


        int fID;
        [Key(true)]
        public int ID
        {
            get { return fID; }
            set { SetPropertyValue<int>(nameof(ID), ref fID, value); }
        }

        FileName fFileName;
        [Association(nameof(FileName) + "-" + nameof(SingleNotCalculated))]
        public FileName FileName
        {
            get { return fFileName; }
            set { SetPropertyValue<FileName>(nameof(FileName), ref fFileName, value); }
        }

        /// <summary>
        /// 单个绝对不计算
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool GetSingleNotCalculated(string fileName)
        {
            try
            {
                var file = XpoCon.UnitOfShort.Query<SingleNotCalculated>().FirstOrDefault(u => u.FileName.FullName == fileName);
                if (file != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                XpoCon.UnitOfShort.RollbackTransaction();
                XpoCon.ReleaseShortUnitOfWork();
                Msg.ErrorMessageShow(ex.Message);
                return false;
            }
            finally
            {
                XpoCon.ReleaseShortUnitOfWork();
            }
        }




        /// <summary>
        /// 检查文件名称并写入单个不检查
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static  bool SetSingleNotCalculated(string fileName)
        { 
            try
            {
                var calculated = XpoCon.UnitOfShort.Query<SingleNotCalculated>().FirstOrDefault(u => u.FileName.FullName == fileName);
                if (calculated == null)
                {
                    calculated = new SingleNotCalculated(XpoCon.UnitOfShort)
                    {
                        FileName = XpoCon.UnitOfShort.Query<FileName>().FirstOrDefault(u => u.FullName == fileName)
                    };
                   
                };
                XpoCon.UnitOfShort.Save(calculated);
                XpoCon.UnitOfShort.CommitChanges();
                return false;
            }
            catch (Exception ex)
            {
                XpoCon.UnitOfShort.RollbackTransaction();
                XpoCon.ReleaseShortUnitOfWork();
                Msg.ErrorMessageShow(ex.Message);
                return false;
            }
            finally
            {
                XpoCon.ReleaseShortUnitOfWork();
            }
        }
    }
}
